package com.peanut.baby.push;

import android.app.ActivityManager;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.text.TextUtils;

import com.peanut.baby.InitManager;
import com.peanut.baby.R;
import com.peanut.baby.model.BaseNotification;
import com.peanut.baby.mvp.bbs.detail.BBSMomentDetailActivity;
import com.peanut.baby.mvp.follow.FollowFansActivity;
import com.peanut.baby.mvp.livedetail.LiveDetailActivity;
import com.peanut.baby.mvp.qadetail.QADetailActivity;
import com.peanut.baby.mvp.web.WebViewActivity;
import com.peanut.devlibrary.util.StringUtil;

/**
 * Created by taransky on 16/6/23.
 */
public class NotiManager {
    private static final String TAG = NotiManager.class.getSimpleName();
    private NotificationManager mManager;
    private Notification mNoti;
    private int 					mNotiId = 0x01;

    private static NotiManager _instance;
    private NotiManager(){}
    public static NotiManager getInstance(){
        if(_instance == null){
            _instance = new NotiManager();
        }
        return _instance;
    }

    private NotificationManager getNotificationManager(){
        if(mManager == null){
            mManager = (NotificationManager) InitManager.getInstance().getCtx().getSystemService(Context.NOTIFICATION_SERVICE);
        }
        return mManager;
    }

    private boolean isAppOnForeground() {
        ActivityManager am = (ActivityManager)InitManager.getInstance().getCtx().getSystemService(Context.ACTIVITY_SERVICE);
        ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
        String currentPackageName = cn.getPackageName();
        if(!TextUtils.isEmpty(currentPackageName) && currentPackageName.equals(InitManager.getInstance().getCtx().getPackageName()))
            return true;
        return false;
    }

    public void showNotification(BaseNotification noti){
        if(noti == null){
            return;
        }

        mNoti = new Notification(R.mipmap.ic_launcher, noti.message, System.currentTimeMillis());
        mNoti.flags |= Notification.FLAG_AUTO_CANCEL;
        mNoti.defaults = Notification.DEFAULT_ALL;

        Intent intent = null;

        if(StringUtil.isNullOrEmpty(noti.url)){
            if(noti.type == BaseNotification.TYPE_LIVE
                    || noti.type == BaseNotification.TYPE_NEW_LIVE){
                intent = new Intent(InitManager.getInstance().getCtx(), LiveDetailActivity.class);  //如果应用未运行点击启动应用
                intent.putExtra("id", Integer.parseInt(noti.bizId));
            }else if(noti.type == BaseNotification.TYPE_NEW_FOLLOWER){
                intent = new Intent(InitManager.getInstance().getCtx(), FollowFansActivity.class);  //如果应用未运行点击启动应用
                intent.putExtra("isFollow",false);
                intent.putExtra("userId",InitManager.getInstance().getUserId());
            }else if(noti.type == BaseNotification.TYPE_QA_REPLY){
                intent = new Intent(InitManager.getInstance().getCtx(), QADetailActivity.class);  //如果应用未运行点击启动应用
                intent.putExtra("id",Integer.parseInt(noti.bizId));
            }else if(noti.type == BaseNotification.TYPE_BBS_REPLY){
                intent = new Intent(InitManager.getInstance().getCtx(), BBSMomentDetailActivity.class);  //如果应用未运行点击启动应用
                intent.putExtra("id",noti.bizId);
            }
        }else{
            intent = new Intent(InitManager.getInstance().getCtx(), WebViewActivity.class);  //如果应用未运行点击启动应用
            intent.putExtra("url", noti.url);
            intent.putExtra("title", noti.message);
        }


        if(intent != null){
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            PendingIntent pendingIntent = PendingIntent.getActivity(InitManager.getInstance().getCtx(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                String channelId = "whatever"; //根据业务执行
                String channelName = "whatever conent"; //这个是channelid 的解释，在安装的时候会展示给用户看
                int importance = NotificationManager.IMPORTANCE_HIGH;
                getNotificationManager().createNotificationChannel(new NotificationChannel(channelId,channelName,importance));

            }

            Notification notification = null;
            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
                notification = new Notification.Builder(InitManager.getInstance().getCtx(),"whatever") //引用加上channelid
                        .setAutoCancel(true)
                        .setSmallIcon(R.mipmap.ic_push_small)
                        .setWhen(System.currentTimeMillis())
                        .setTicker(noti.message)
                        .setContentText(noti.message)
                        .setContentTitle(noti.title)
                        .setSmallIcon(R.mipmap.ic_push_small)
                        .setContentIntent(pendingIntent)
                        .build();
            }else{
                notification = new Notification.Builder(InitManager.getInstance().getCtx())
                        .setAutoCancel(true)
                        .setSmallIcon(R.mipmap.ic_push_small)
                        .setWhen(System.currentTimeMillis())
                        .setTicker(noti.message)
                        .setContentText(noti.message)
                        .setContentTitle(noti.title)
                        .setSmallIcon(R.mipmap.ic_push_small)
                        .setContentIntent(pendingIntent)
                        .build();
            }

            getNotificationManager().notify(1,notification);
        }

    }

    private boolean isAppRunning() {
//        ActivityManager am = (ActivityManager) InitManager.getInstance().getContext()
//                .getSystemService(Context.ACTIVITY_SERVICE);
//        List<ActivityManager.RunningTaskInfo> list = am.getRunningTasks(100);
//        for (ActivityManager.RunningTaskInfo info : list) {
//            if (info.baseActivity.getPackageName().equals(InitManager.getInstance().getContext().getPackageName())) {
//                return true;
//            }
//        }
        return false;
    }



    public void clear(int id){
        try {

            getNotificationManager().cancel(id);
        } catch (Exception e) {

        }
    }


    /**
     *
     */
    public void clear(){
        try {
            getNotificationManager().cancel(mNotiId);
            mNoti = null;
        } catch (Exception e) {

        }
    }

}
